package com.wanmait.myhouse.managerController;
import com.wanmait.myhouse.dao.ManagerDAO;
import com.wanmait.myhouse.util.CookieUtils;
import com.wanmait.myhouse.vo.Manager;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("manager")
public class ManagerLoginController {

    @PostMapping("verifyLogin")
    public String verifyLogin(Manager manager, HttpSession session , String autoLogin , HttpServletResponse response){
        ManagerDAO managerDAO = new ManagerDAO();
        Manager man = managerDAO.verLogin(manager);
        if (man == null){
            return "redirect:/manager/login";
        }else {

            if ("1".equals(autoLogin)){
                CookieUtils.createCookie("managerName",man.getManagerName(),7*24*60*60,"/",response);
                CookieUtils.createCookie("managerPass",man.getManagerPass(),7*24*60*60,"/",response);
            }
            session.setAttribute("manager",man);
            return "redirect:/manager/index";
        }
    }

    @GetMapping("exit")
    public String exit(HttpSession session , HttpServletResponse response){
        CookieUtils.createCookie("managerName","",0,"/",response);
        CookieUtils.createCookie("managerPass","",0,"/",response);
        session.invalidate();
        return "redirect:/manager/login";
    }

    @GetMapping("login")
    public String login(Manager manager){
        return "manager/login";
    }
}
